package com.taojin.shoppinginterest.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnRadioGroupCheckedChange;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.adapter.HomeFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by T400 on 15-5-11.
 */
public class HomeFragment extends Fragment implements ViewPager.OnPageChangeListener{
    @ViewInject(R.id.viewpager_homefragment)
    private ViewPager viewPager;
    private List<Fragment> fragments;
    private HomeFragmentAdapter homeFragmentAdapter;
    @ViewInject(R.id.rg_top_home)
    private  RadioGroup radioGroupTop;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.activity_homefragment,container,false);
        ViewUtils.inject(this,view);
        initViewPager();
        initData();
        return view;

    }

    private void initData() {
        homeFragmentAdapter=new HomeFragmentAdapter(fragments,getActivity().getSupportFragmentManager());
        viewPager.setAdapter(homeFragmentAdapter);
    }

    private void initViewPager() {
        fragments=new ArrayList<Fragment>();
        fragments.add(new SelectionFragment());
        fragments.add(new SubjectFragment());
        fragments.add(new AttentionFragment());
        fragments.add(new ListFragment());
        viewPager.setOnPageChangeListener(this);

    }

    @OnClick({R.id.rd_selection_top,R.id.rd_subject_top,R.id.rd_attention_top,R.id.rd_list_top})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rd_selection_top:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rd_subject_top:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rd_attention_top:
                viewPager.setCurrentItem(2);
                break;
            case R.id.rd_list_top:
                viewPager.setCurrentItem(3);
                break;
            default:
                break;
        }


    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton rbCheck= (RadioButton) radioGroupTop.getChildAt(position);
        rbCheck.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
